package com.tobby.camel.camelinaction.chapter6.firsttest;

import java.io.File;

import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CamelRiderTest extends CamelSpringTestSupport {

	private String inboxDir;

	private String outBoxDir;

	@EndpointInject(uri = "file:{{file.inbox}}")
	private ProducerTemplate inbox;

	public void setUp() throws Exception {
		super.setUp();
		inboxDir = context.resolvePropertyPlaceholders("{{file.inbox}}");
		outBoxDir = context.resolvePropertyPlaceholders("{{file.outbox}}");
		deleteDirectory(inboxDir);
		deleteDirectory(outBoxDir);
	}

	@Override
	protected AbstractApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext(
				"com/tobby/camel/camelinaction/chapter6/firsttest/rider-camel-prod.xml",
				"com/tobby/camel/camelinaction/chapter6/firsttest/rider-camel-test.xml");
	}

	@Test
	public void testMoveFile() throws InterruptedException {
		inbox.sendBodyAndHeader("Hello World", Exchange.FILE_NAME, "hello.txt");
		Thread.sleep(1000);

		File target = new File(outBoxDir + "/hello.txt");
		assertTrue("File not moved", target.exists());

		String content = context.getTypeConverter().convertTo(String.class, target);
		assertEquals("Hello World", content);
	}

}
